﻿/// <reference path="jquery-1.7.1-vsdoc.js" />

function buildCombo(selectedValues) {
    /*
        selectedValues.cb1, selectedValues.cb2, selectedValues.cb3 
    */

    $('#filter').html('');

    var select1 = $('<select></select>');
    select1.attr('id', 'cb1').attr('name', 'cb1');

    var select2 = $('<select></select>');
    select2.attr('id', 'cb2').attr('name', 'cb2');

    var select3 = $('<select></select>');
    select3.attr('id', 'cb3').attr('name', 'cb3');

    $('#filter').append(select1).append(select2).append(select3);


    select1.change(function () {
        var combo1 = $('#cb1');
        if (combo1.val() == null || combo1.val() == 10) {
            // when we select ALL => reset all others (reload)
            bindCombos("2", null, selectedValues);
            bindCombos("3", null, selectedValues);
        }
        else {
            // reload sub-categories for combo1 in combo2, combo3
            bindCombos("2", combo1.val(), selectedValues);
            bindCombos("3", null, selectedValues);
        }

    });

    select2.change(function () {
        var combo2 = $('#cb2');
        if (combo2.val() == null || combo2.val() == 20) {
            // when we select ALL => reset all others (reload)
            bindCombos("3", null, selectedValues);
        }
        else {
            // reload sub-categories for combo2 in combo3
            bindCombos("3", combo2.val(), selectedValues);
        }
    })

    // trigger binding for the first combo
    bindCombos("1", null, selectedValues);
}

function bindCombos(comboType, subcategory, selectedValues) {
    var comboUrl = "/Home/Category?";
    comboUrl += "code=" + comboType;

    // only if subcategory is defined
    if (subcategory != null) {
        comboUrl += "&subcategory=" + subcategory;
    }

    var combo = $('#cb' + comboType);

    $.ajax({
        type: "GET",
        dataType: "json",
        url: comboUrl,
        success: function (data) {
            // clear data
            combo.html('');

            // add data
            for (var i = 0; i < data.length; i++) {

                var option = $("<option></option")
                    .text(data[i].text)
                    .attr('value', data[i].val);

                if ((comboType == 1 && data[i].val == selectedValues.cb1)
                    || (comboType == 2 && data[i].val == selectedValues.cb2)
                    || (comboType == 3 && data[i].val == selectedValues.cb3)) {

                    option.attr('selected', 'selected');
                }
                else {
                    option.removeAttr('selected');
                }

                combo.append(option);
            }

            // notify change
            combo.change();
        }
    });

   
}

function addToCombo(select, data, selectedValues) {

    for (var i = 0; i < data.length; i++) {
        var option = $("<option></option");
        option.text(data[i].text);
        option.attr('value', data[i].val);

        select.append(option);
    }
}
